﻿using System;
using System.Reflection;

using Unity.Lifetime;

using pri.smilly.QuickStart.Aop;

namespace pri.smilly.QuickStart.Ioc {

    public class BeanWrapper {
        public string Name { private set; get; }
        public Type From { private set; get; }
        public Type MapTo { private set; get; }
        public ITypeLifetimeManager LifeTimeManager { private set; get; }
        public object Instance { private set; get; }

        public bool IsPonitCut { get => IsPointCut(From) || IsPointCut(MapTo); }
        private bool IsPointCut(Type type) => type?.GetCustomAttribute(typeof(PointCutAttribute)) != null;
        public bool IsInstance { get => null != Instance; }

        public LifeTime LifeTime {
            set {
                switch (value) {
                    case LifeTime.Singleton:
                        LifeTimeManager = new ContainerControlledLifetimeManager();
                        break;
                    case LifeTime.ThreadLocal:
                        LifeTimeManager = new ContainerControlledTransientManager();
                        break;
                    case LifeTime.Transient:
                        LifeTimeManager = new PerThreadLifetimeManager();
                        break;
                }
            }
        }

        public BeanWrapper() { }

        public BeanWrapper(string name, Type from, Type mapTo) {
            Name = name;
            From = from;
            MapTo = mapTo;
        }

        public BeanWrapper(string name, Type from, object instance) {
            Name = name;
            From = from;
            Instance = instance;
        }
    }
}
